﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="recAcceptanceform.aspx.cs"
    Inherits="SIPO.Web.forms.Transaction.recAcceptanceform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script src="<%= ResolveUrl("~/scripts/globalization/globalize.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/globalization/cultures/globalize.culture.id-ID.js") %>"
        type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript">

        $(document).ready(function () {

            Globalize.culture("id-ID");

        });
    
    </script>
    <script type="text/javascript">

        function tampilTreeView(acceptanceId) {

            $('#btnNext').hide();
            $('#btnCancel').hide();
            $('#btnClose').show();
            $('#btnSave1').show();
            $('#btnApprove1').hide();
            $('#fsPeriode').hide();
            $('#fsDetailAcceptance').show();

            var review = '<%= Request.Params["review"] %>';

            if (review == 1) {
                $('#btnSave1').hide();
                $('#btnApprove1').show();
            }

            $('#txtPeriode').attr('disabled', 'disabled');
            $('#txtVersi').attr('disabled', 'disabled');

            var valuesbefore = 0;

            var id = '<%= Request.Params["id"] %>';

            var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recAcceptanceDetailhandler.ashx") %>' + '?method=getdata&acceptanceid=' + id;

            if (id == '' || id == '0')
                handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recAcceptanceDetailhandler.ashx") %>' + '?method=getdatahierarchy';

            var view = '<%= Request.Params["view"] %>';
            if (view == '1') {
                $('#btnSave1').hide();
            }

            $('#table1').jqGrid({
                url: handlerUrl,
                datatype: 'json',
                colNames: ['ID', 'Jenis Usaha', 'Target', 'Level', 'ParentID', 'KeyCode', 'IsLeaf', 'IsExpand'],
                colModel: [
           		{ name: 'ID', key: true, hidden: true, index: 'ID', width: 60 },
           		{ name: 'Description', index: 'Description', width: 400 },
           		{ name: 'Values', index: 'Values', width: 180, editable: true, edittype: 'text', align: 'right', formatter: formatCurrency, unformat: unformatCurrency, editoptions: {
           		    dataInit: function (element) {
           		        $(element).inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });
           		    }
           		}
           		},
                { name: 'Level', index: 'Level', width: 200, hidden: true },
                { name: 'ParentID', index: 'ParentID', width: 200, hidden: true },
                { name: 'KeyCode', index: 'KeyCode', width: 200, hidden: true },
                { name: 'IsLeaf', index: 'IsLeaf', width: 30, hidden: true },
                { name: 'IsExpand', index: 'IsExpand', width: 30, hidden: true }
           	],
                treeGrid: true,
                treeGridModel: 'adjacency',
                ExpandColumn: 'Description',
                id: 'ID',
                multiselect: true,
                treedatatype: 'json',
                sortname: 'ID',
                sortorder: "asc",
                viewrecords: true,
                jsonReader: { repeatitems: false },
                height: '100%',
                caption: 'Target Penerimaan',
                ajaxGridOptions: { cache: false },
                cellEdit: true,
                cellsubmit: 'clientArray',
                beforeSelectRow: function (rowid, e) {
                    var obj = $('#table1').jqGrid('getRowData', rowid);
                    if (obj.Level == 3 && review != '1' && (view == '' || view == '0')) {
                        return true;
                    }
                    else {
                        return false;
                    }
                },
                beforeEditCell: function (rowid, e) {
                    valuesbefore = Globalize.parseFloat($('#table1').jqGrid('getCell', rowid, 'Values'));
                },
                afterSaveCell: function (rowid, e) {
                    var grid = $('#table1');
                    var parent = grid.jqGrid('getCell', rowid, 'ParentID');

                    var values = Globalize.parseFloat(grid.jqGrid('getCell', rowid, 'Values'));

                    values = values - valuesbefore;

                    var parentUp = grid.jqGrid('getCell', parent, 'ParentID');
                    var valuesParentbefore = Globalize.parseFloat(grid.jqGrid('getCell', parent, 'Values'));
                    var valuesfirstparent = Globalize.parseFloat(grid.jqGrid('getCell', parentUp, 'Values'));

                    //edit par 2
                    grid.jqGrid('setCell', parent, 'Values', valuesParentbefore + values);
                    var valuesParentafter = Globalize.parseFloat(grid.jqGrid('getCell', parent, 'Values'));
                    valuesParentafter = valuesParentafter - valuesParentbefore;

                    //edit par 1
                    grid.jqGrid('setCell', parentUp, 'Values', valuesfirstparent + valuesParentafter);

                    if (valuesfirstparent + valuesParentafter > 999999999999999) {
                        alert("Nilai terlalu besar !");
                        grid.jqGrid('setCell', parent, 'Values', valuesParentbefore);
                        grid.jqGrid('setCell', parentUp, 'Values', valuesfirstparent);
                        grid.jqGrid('setCell', rowid, 'Values', valuesbefore);
                    }
                },
                loadComplete: function () {
                    grid = jQuery("#table1");
                    var ids = grid.jqGrid('getDataIDs');
                    for (var i = 0; i < ids.length; i++) {
                        var rowId = ids[i];
                        if (grid.jqGrid('getCell', rowId, 'Level') == 1) {
                            grid.jqGrid('setCell', rowId, "Values", "", { 'font-weight': 'bold', 'font-size': '11.5px' });
                        }
                        else if (grid.jqGrid('getCell', rowId, 'Level') == 2) {
                            grid.jqGrid('setCell', rowId, "Values", "", { 'font-weight': 'bold', 'font-size': '11px' });
                        }
                    }
                },
                treeReader: {
                    level_field: "Level",
                    parent_id_field: "ParentID",
                    leaf_field: "IsLeaf",
                    expanded_field: "IsExpand",
                    loaded: true
                }
            });

            function formatCurrency(cellvalue, options, rowObject) {

                if (cellvalue == '' || cellvalue == undefined || cellvalue == 0)
                    cellvalue = '0,00';

                return Globalize.format(cellvalue, "n2"); ;

            }

            function unformatCurrency(cellvalue, options) {

                if (cellvalue == '' || cellvalue == undefined)
                    cellvalue = '0,00';

                return cellvalue;
            }

        }
        $(document).ready(function () {

            $("#btnNext").button({ icons: { primary: "ui-icon-circle-arrow-e"} });
            $("#btnCancel").button({ icons: { primary: "ui-icon-close"} });
            $("#btnClose").button({ icons: { primary: "ui-icon-close"} });
            $("#btnSave1").button({ icons: { primary: "ui-icon-disk"} });
            $("#btnApprove1").button({ icons: { primary: "ui-icon-pencil"} });

            var id = $('#hfAcceptanceId').val();

            if (id == 0) {
                $("#btnSubmit").hide();
                $("#btnClose").hide();
                $("#btnSave1").hide();
                $("#fsDetailAcceptance").hide();
                $('#btnApprove1').hide();
                $('#txtPeriode').attr("disabled", "disabled");
            } else {
                tampilTreeView(id);
            }

        });
    </script>
    <script type="text/javascript">

        $(document).ready(function () {

            // 1. prepare the validation rules and messages.
            var rules = {
                txtNoSk: {
                    required: true
                },
                txtTglSk: {
                    required: true
                },
                ddlPeriode: "required"
            };
            var messages = {
                txtNoSk: {
                    required: "No. S.K. harus di isi"
                },
                txtTglSk: {
                    required: "Tanggal S.K. harus di isi"
                },
                ddlPeriode: "Periode harus di isi"
            };

            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);

            var date = new Date();
            var currentMonth = date.getMonth();
            var currentDate = date.getDate();
            var currentYear = date.getFullYear();

            $("#txtTglSk").datepicker({ showWeek: true, showButtonPanel: true,
                changeMonth: true, changeYear: true, dateFormat: 'dd/mm/yy',
                maxDate: new Date(currentYear, currentMonth, currentDate)
            });

            $("#txtTglSk").inputmask("d/m/y", { "placeholder": "dd/mm/yyyy" });

            $('#btnNext').click(function () {

                if (!validator.validate()) {
                    return;
                }
                else {

                    var skNo = $('#txtNoSk').val();

                    var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/Loaddata/validasiSkNo.ashx?SkNo=") %>';

                    $.ajax({
                        type: 'POST',
                        url: handlerUrl + skNo + "&review=",
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {

                            if (data.jumlah > 0) {
                                $('#txtNoSk').val('');
                                $('#txtNoSk').focus();
                                $("#txtNoSk").css({ 'background-color': 'red' });
                            }
                            else {

                                $("#txtNoSk").css({ 'background-color': 'white' });

                                var periode = $('#ddlPeriode').val();

                                var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/Loaddata/validasiAcceptanceDate.ashx?Period=") %>';

                                $.ajax({
                                    type: 'POST',
                                    url: handlerUrl + periode,
                                    contentType: 'application/json',
                                    dataType: 'json',
                                    success: function (data) {
                                        if (data.jumlah > 0) {
                                            alert("periode sudah ada !");
                                            $("#ddlPeriode").css({ 'background-color': 'red' });
                                        } else {
                                            $("#ddlPeriode").css({ 'background-color': 'white' });

                                            $('#txtPeriode').val($("#ddlPeriode").val());
                                            $('#txtVersi').val('1');
                                            tampilTreeView(0);
                                        }
                                    },
                                    error: function () {
                                        // alert('Error validasi periode!');
                                    }
                                });

                            }
                        },
                        error: function () {
                            alert('Error validasi no. S.K. !');
                        }
                    });
                }
            });


            $('#ddlPeriode').change(function () {

                var periode = $('#ddlPeriode').val();

                var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/Loaddata/validasiAcceptanceDate.ashx?Period=") %>';

                $.ajax({
                    type: 'POST',
                    url: handlerUrl + periode,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (data) {
                        if (data.jumlah > 0) {
                            alert("periode sudah ada !");
                            $("#ddlPeriode").css({ 'background-color': 'red' });
                        } else {
                            $("#ddlPeriode").css({ 'background-color': 'white' });
                        }
                    },
                    error: function () {
                        // alert('Error validasi periode!');
                    }
                });

            });

            $('#txtNoSk').change(function () {

                var skNo = $('#txtNoSk').val();

                var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/Loaddata/validasiSkNo.ashx?SkNo=") %>';

                $.ajax({
                    type: 'POST',
                    url: handlerUrl + skNo,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (data) {
                        if (data.jumlah > 0) {
                            alert("Nomor S.K. sudah ada !");
                            $('#txtNoSk').val('');
                            $('#txtNoSk').focus();
                            $("#txtNoSk").css({ 'background-color': 'red' });
                        } else {
                            $("#txtNoSk").css({ 'background-color': 'white' });
                        }

                    },
                    error: function () {
                        alert('Error validasi no. S.K. !');
                    }
                });
            });

            $('#btnCancel').click(function () {

                $('#txtNoSk').val('');
                $('#txtTglSk').val('');
                $('#ddlPeriode').val('');

                parent.$('#dialog1').dialog('close');

                return false;
            });

            $('#btnClose').click(function () {

                $('#txtNoSk').val('');
                $('#txtTglSk').val('');
                $('#ddlPeriode').val('');

                parent.$('#dialog1').dialog('close');

                return false;
            });

            $('#txtNoSk').upperCase();

        });
    </script>
    <script type="text/javascript">

        var grid = $('#table1');

        $("#btnApprove1").click(function () {

            var ids = $("#table1").jqGrid('getDataIDs');
            var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/transaction/recAcceptancehandler.ashx") %>';

            var id = '<%= Request.Params["id"] %>';

            if (id > 0) {

                CallAjax(handlerUrl, "?method=review&acceptanceid=" + id);

                parent.$('#dialog1').dialog('close');

                setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                return false;
            }
        });

        $("#btnSave1").click(function () {

            var list = [];
            var total = 0;
            var ids = $("#table1").jqGrid('getDataIDs');

            for (var i = 0; i < ids.length; i++) {

                var rowId = ids[i];

                var objGrid = $('#table1').jqGrid('getRowData', rowId);

                var id = objGrid.ID;
                var description = objGrid.Description;
                var values = objGrid.Values;
                var level = objGrid.Level;
                var keyCode = objGrid.KeyCode;
                var isLeaf = objGrid.IsLeaf;
                var isExpand = objGrid.IsExpand;

                if (values.toString() != '') {

                    var recAcceptanceDetail = new Object();
                    recAcceptanceDetail.ID = encodeURIComponent(id);
                    recAcceptanceDetail.Description = encodeURIComponent(description);
                    recAcceptanceDetail.Values = encodeURIComponent(values);
                    recAcceptanceDetail.KeyCode = encodeURIComponent(keyCode);
                    recAcceptanceDetail.Level = encodeURIComponent(level);
                    recAcceptanceDetail.IsLeaf = encodeURIComponent(isLeaf);
                    recAcceptanceDetail.IsExpand = encodeURIComponent(isExpand);
                    list.push(recAcceptanceDetail);

                    if (level == 1) {

                        total += Globalize.parseInt(values);
                        if (total > 999999999999999) {
                            alert('Nilai total terlalu besar !');
                            $("#btnSave1").show();
                            selText = [];
                            total = 0;
                            return false;
                            break;
                        }
                    }
                }
                else {
                    alert('Selesaikan proses input terlebih dahulu !');
                    $("#btnSave1").show();
                    list = [];
                    return false;
                    break;
                }
            }

            var periode = $('#txtPeriode').val();
            var versi = $('#txtVersi').val();
            var noSk = $('#txtNoSk').val();
            var tglSk = $('#txtTglSk').val();

            var objectRecAcceptance = new Object();
            objectRecAcceptance.AcceptancePeriod = encodeURIComponent(periode);
            objectRecAcceptance.Versi = encodeURIComponent(versi);
            objectRecAcceptance.SkNo = encodeURIComponent(noSk);
            objectRecAcceptance.SkDate = encodeURIComponent(tglSk);
            objectRecAcceptance.Details = list;

            jsonData = JSON.stringify(objectRecAcceptance);

            var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/transaction/recAcceptancehandler.ashx") %>';

            CallAjax(handlerUrl, '?method=insert', jsonData);

            parent.$('#dialog1').dialog('close');

            setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <fieldset id="fsPeriode">
        <legend><b>Input Periode</b></legend>
        <div>
            <table>
                <tr>
                    <td>
                        Tahun
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlPeriode" runat="server">
                        </asp:DropDownList>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                </tr>
                <tr>
                    <td>
                        No. Surat
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtNoSk" runat="server"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                </tr>
                <tr>
                    <td>
                        Tgl. Surat
                    </td>
                    <td>
                        :
                    </td>
                    <td>
                        <asp:TextBox ID="txtTglSk" runat="server"></asp:TextBox>
                    </td>
                    <td class="bintang">
                        *
                    </td>
                </tr>
            </table>
        </div>
    </fieldset>
    <br />
    <fieldset id="fsDetailAcceptance">
        <div style="width: 778px; float: left;">
            <b>Target Penerimaan Tahun : </b>
            <input type="text" id="txtPeriode" runat="server" size='5' />&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Versi : </b>
            <input type="text" id="txtVersi" size='5' runat="server" />
            <div>
                <table style="height: 20px">
                    <tr>
                    </tr>
                </table>
            </div>
            <div style="width: 773px">
                <table id="table1" class="scroll" cellpadding="0" cellspacing="0">
                </table>
                <div id="pager" class="scroll">
                </div>
            </div>
        </div>
    </fieldset>
    <br />
    <table border="0">
        <tr align="right">
            <td colspan="2">
            </td>
            <td>
                <div style="text-align: right; padding-top: 10px;">
                    <button id="btnCancel" type="button">
                        Batal</button>
                    <button id="btnClose" type="button">
                        Tutup</button>
                    <button id="btnNext" type="button">
                        Lanjut</button>
                    <button id="btnSave1" type="button">
                        Simpan</button>
                    <button id="btnApprove1" type="button">
                        Setujui
                    </button>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <asp:HiddenField ID="hfAcceptanceId" runat="server" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
